package com.ala4.oxcafe.repository;

import com.ala4.oxcafe.boot.page.PageDomain;
import com.ala4.oxcafe.common.utils.object.ObjectUtil;
import com.ala4.oxcafe.common.utils.text.StrUtil;
import com.ala4.oxcafe.entity.RoleInfo;
import com.ala4.oxcafe.exception.BusinessException;
import com.ala4.oxcafe.repository.mapper.RoleInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 角色信息
 *
 * @author PING
 * @date 2025-03-25
 */
@Repository
public class RoleInfoRepository extends ServiceImpl<RoleInfoMapper, RoleInfo> implements IService<RoleInfo> {

    public IPage<RoleInfo> selectRoleInfoPage(PageDomain pageDomain, RoleInfo roleInfo) {

        LambdaQueryWrapper<RoleInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(roleInfo.getRoleName()), RoleInfo::getRoleName, roleInfo.getRoleName());
        queryWrapper.eq(ObjectUtil.isNotNull(roleInfo.getStatus()), RoleInfo::getStatus, roleInfo.getStatus());

        return this.page(pageDomain.toPlusPage(), queryWrapper);
    }

    public List<RoleInfo> selectRoleCode(String roleCode) {
        return this.list(new LambdaQueryWrapper<RoleInfo>().eq(RoleInfo::getRoleCode, roleCode));
    }

    /**
     * 获取门店管理员角色
     *
     * @return
     */
    public RoleInfo selectStoreAdmin() {
        RoleInfo roleInfo = this.getOne(Wrappers.<RoleInfo>lambdaQuery().eq(RoleInfo::getRoleCode, "R_ADMIN"));
        if (ObjectUtil.isNull(roleInfo)) {
            throw new BusinessException("获取门店管理员角色出错,请联系管理员查看是否有门店管理员角色");
        }
        return roleInfo;
    }
}
